package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.MchntInfoMapper;
import cc.rengu.igas.momp.common.entity.TBMchntBaseInfo;
import cc.rengu.igas.momp.common.entity.TBMchntFeeInfo;
import cc.rengu.igas.momp.common.entity.TBMchntSignInfo;
import cc.rengu.igas.momp.common.enums.MchntTypeEnum;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户信息数据库操作接口实现
 *
 * @author Jinan Liu
 * @date 2020/4/7 17:03
 */
public class MchntInfoMapperImpl implements MchntInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    /**
     * 根据法人机构号和商户编号查询商户基本信息数据库操作接口实现
     *
     * @param instId  法人机构号
     * @param mchntNo 商户编号
     * @return cc.rengu.igas.momp.common.entity.TBMchntBaseInfo 商户基本信息表T_B_MCHNT_BASE_INFO
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 17:07
     */
    @Override
    public TBMchntBaseInfo selectMompMchntBaseInfoByPrimaryKey(String instId, String mchntNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMompMchntBaseInfoByPK");

        TBMchntBaseInfo mchntBaseInfo = new TBMchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setMchntNo(mchntNo);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBaseInfo, mchntBaseInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户基本信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }

        return mchntBaseInfo;
    }

    /**
     * 查询机构下所有商户信息
     *
     * @param instId     法人机构号
     * @param signInstId 签约机构
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TBMchntBaseInfo> 商户基本信息表T_B_MCHNT_BASE_INFO
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @since 2020-06-30 01:57
     */
    @Override
    public List<TBMchntBaseInfo> selectMchntBaseInfoListBySignInstId(String instId, String signInstId) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntBaseInfoListBySignInstId");

        // 按接口赋查询条件
        TBMchntBaseInfo tbMchntBaseInfo = new TBMchntBaseInfo();
        tbMchntBaseInfo.setInstId(instId);
        tbMchntBaseInfo.setSignInstId(signInstId);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tbMchntBaseInfo, TBMchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询法人机构<{}>的机构<{}>下的签约商户信息失败!", instId, signInstId);
            return null;
        }

        return objectList.stream().map(item -> (TBMchntBaseInfo) (item)).collect(Collectors.toList());
    }

    /**
     * @Author: liujinan
     * @Date: 2020/7/14
     * @Description: 查询商户基本信息
     * @param: instId
     * @param: mcNo
     * @param: mcName
     * @Return: java.util.List<cc.rengu.igas.momp.common.entity.StatToClientDay>
     * @Exception:
     */
    @Override
    public TBMchntBaseInfo selectMchntBaseInfoByMchntNo(String instId, String mchntNo) throws Exception {
        String dbPoolNameMCMP = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolNameMCMP = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolNameMCMP);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectMchntBaseInfo");
        TBMchntBaseInfo mchntBaseInfo = new TBMchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setMchntNo(mchntNo);
        //00-非正式商户01-正式商户02-停用商户 03-已注销商户
        mchntBaseInfo.setMchntStatus("01");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBaseInfo, mchntBaseInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户基本信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }
        return mchntBaseInfo;
    }

    /**
     * 查询商户所有费率信息
     *
     * @param instId  法人机构号
     * @param mchntNo 商户号
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TBMchntFeeInfo> 商户费率信息表T_B_MCHNT_FEE_INFO
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @since 2020-06-30 01:57
     */
    @Override
    public List<TBMchntFeeInfo> selectMchntFeeInfoListByMchntNo(String instId, String mchntNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntFeeInfoListByMchntNo");

        // 按接口赋查询条件
        TBMchntFeeInfo tbMchntFeeInfo = new TBMchntFeeInfo();
        tbMchntFeeInfo.setInstId(instId);
        tbMchntFeeInfo.setMchntNo(mchntNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tbMchntFeeInfo, TBMchntFeeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询商户<{}>下的费率信息失败!", mchntNo);
            return null;
        }

        return objectList.stream().map(item -> (TBMchntFeeInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 查询机构下所有商户信息
     *
     * @param instId    法人机构号
     * @param mchntArea 商圈号
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TBMchntBaseInfo> 商户基本信息表T_B_MCHNT_BASE_INFO
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @since 2020-06-30 01:57
     */
    @Override
    public List<TBMchntBaseInfo> selectMchntBaseInfoListByMchntArea(String instId, String mchntArea) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntBaseInfoListByMchntArea");

        // 按接口赋查询条件
        TBMchntBaseInfo tbMchntBaseInfo = new TBMchntBaseInfo();
        tbMchntBaseInfo.setInstId(instId);
        tbMchntBaseInfo.setMchntArea(mchntArea);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tbMchntBaseInfo, TBMchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询法人机构<{}>的商圈<{}>下的签约商户信息失败!", instId, mchntArea);
            return null;
        }

        return objectList.stream().map(item -> (TBMchntBaseInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 查询商户签约信息
     *
     * @param instId  成员行号
     * @param mchntNo 商户编号
     * @return
     * @throws Exception
     * @author liujinan
     * @since 2021-05-28 10:57
     */
    @Override
    public List<TBMchntSignInfo> selectMchntSignInfoByPrimaryPK(String instId, String mchntNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntSignInfoByPK");

        // 按接口赋查询条件
        TBMchntSignInfo tbMchntSignInfo = new TBMchntSignInfo();
        tbMchntSignInfo.setInstId(instId);
        tbMchntSignInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tbMchntSignInfo, TBMchntSignInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("法人机构<{}>查询商户<{}>签约信息查询异常，未查询到该商户签约信息!", tbMchntSignInfo.getInstId(), mchntNo);
            return null;
        }

        return objectList.stream().map(item -> (TBMchntSignInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 根据商户签约信息查询重复入网商户列表
     *
     * @param instId       成员行号
     * @param mchntNo      商户编号
     * @param artifCertNo  证件号码
     * @param licenceCode  营业执照
     * @param settleAcctNo 结算账户
     * @return
     * @throws Exception
     */
    @Override
    public List<TBMchntSignInfo> selectRepeatMchntBySignInfo(String instId, String mchntNo, String artifCertNo, String licenceCode, String settleAcctNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = "";

        // 按接口赋查询条件
        TBMchntSignInfo mchntSignInfo = new TBMchntSignInfo();
        mchntSignInfo.setInstId(instId);
        mchntSignInfo.setArtifCertNo(artifCertNo);
        mchntSignInfo.setLicenceCode(licenceCode);
        mchntSignInfo.setSettleAcctNo(settleAcctNo);

        sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntSignInfoByCertNo");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntSignInfo, TBMchntSignInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("法人机构<{}>查询商户<{}>一年内签约信息一致的商户为空!", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (TBMchntSignInfo) (item)).collect(Collectors.toList());
    }

}
